package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.entity.PmsAttr;
import com.shop.mapper.PmsAttrMapper;
import com.shop.service.PmsAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 分类属性表 服务实现类
 * </p>
 *
 * @author lpx610
 * @since 2023-11-04
 */
@Service
public class PmsAttrServiceImpl extends ServiceImpl<PmsAttrMapper, PmsAttr> implements PmsAttrService {

    @Override
    public List<PmsAttr> getByCategoryId(String categoryId) {
        QueryWrapper<PmsAttr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId).orderByAsc("sort");
        return this.list(queryWrapper);
    }

    @Override
    @Transactional
    public Boolean add(String[] names, String categoryId, Integer type) {
        String[] nameArray = StringUtils.strip(Arrays.toString(names), " []").split(", ");
        List<PmsAttr> list = new ArrayList<>();
        for (String name : nameArray) {
            PmsAttr pmsAttr = new PmsAttr();
            list.add(pmsAttr.setName(name).setCategoryId(categoryId).setType(type));
        }
        return this.saveBatch(list);
    }

    @Override
    public Boolean update(String id, String name, Integer type) {
        PmsAttr pmsAttr = new PmsAttr(id);
        pmsAttr.setName(name)
                .setType(type);
        return this.updateById(pmsAttr);
    }

    @Override
    public Boolean delete(String id) {
        return this.removeById(id);
    }

}
